package com.shelpe.services.sinbadgateway.entity.jpa;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;

import com.shelpe.services.sinbadgateway.entity.jpa.AutoProductQueue.QueuePK;
import com.shelpe.services.sinbadgateway.utils.EntityUtils;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Entity(name="auto_pro_product_queue")
@IdClass(QueuePK.class)
@Setter
@Getter
public class AutoProductQueue {

	@Id
	@Column(name="product_id")
	private long productID;
	@Id
	@Column(name="campaign_id")
	private long campaignID;
	
	@Column(name="title1")
	private String firstCreativeName = "";
	@Column(name="img_url1")
	private String firstCreativeUrl = "";
	@Column(name="title2")
	private String secondCreativeName = "";
	@Column(name="img_url2")
	private String secondCreativeUrl = "";
	
	@Column(name="user_id")
	private long userID;
	@Column(name="adgroup_id")
	private long adgroupID;
	private int defaultPrice;
	@Column(name="max_expense")
	private int maxPrice;
	
	private int flag;
	
	private String candWords;
	private String usedWords;
	
	private String strategy = "V2";
	private boolean needConfirm;
	
	private int progress;
	@Column(name="msg")
	private String message = "";
	
	@Convert(converter = StatusConverter.class)
	private Status status;
	@Convert(converter = DriveTypeConverter.class)
	private DriveType type;
	
	@Column(name="addtime")
	@Convert(converter=EntityUtils.Day2IntConverter.class)
	private Date createTime;
	@Column(name="updated")
	@Convert(converter=EntityUtils.Day2IntConverter.class)
	private Date lastModifiedTime;
	@Convert(converter=EntityUtils.Day2IntConverter.class)
	private Date sequence;
	
	@Data
	public static class QueuePK implements Serializable{
		private static final long serialVersionUID = 1L;
		@Column(name="product_id")
		private long productID;
		@Column(name="campaign_id")
		private long campaignID;
	}
	
	public static enum Status{
		added(0), updating(1), success(2), failed(-1);
		
		private int value;
		
		private Status(int v){
			this.value = v;
		}
		
		public int getValue(){
			return this.value;
		}
		
	}
	
	public static class StatusConverter implements AttributeConverter<Status, Integer>{

		@Override
		public Integer convertToDatabaseColumn(Status attribute) {
			return attribute!=null?attribute.value:Status.failed.getValue();
		}

		@Override
		public Status convertToEntityAttribute(Integer dbData) {
			Status status = Status.failed;
			Status.values();
			for(Status s: Status.values()){
				if(s.getValue() == dbData){
					status = s;
					break;
				}
			}
			return status;
		}
	}
	
	public static enum DriveType{
		smart(1), click(2), ppc(3), wide(4);
		private int value;
		
		private DriveType(int v){
			this.value = v;
		}
		
		public int getValue(){
			return this.value;
		}
		
		public static DriveType from(int value){
			DriveType type = DriveType.smart;
			for(DriveType t: DriveType.values()){
				if(t.getValue() == value){
					type = t;
					break;
				}
			}
			return type;
		}
	}
	
	public static class DriveTypeConverter implements AttributeConverter<DriveType, Integer>{

		@Override
		public Integer convertToDatabaseColumn(DriveType attribute) {
			return attribute!=null?attribute.value:DriveType.smart.getValue();
		}

		@Override
		public DriveType convertToEntityAttribute(Integer dbData) {
			return DriveType.from(dbData);
		}
	}
}
